import { sys } from "cc";
import { IUserData } from "./UserModel";

export enum LocalKey {
    // 用户数据
    userData = "userData",
    // 语言
    language = "language",
    // // 音乐
    // music = "music",
    // // 音效
    // sound = "sound",
    // // 震动
    // shake = "shake",
    // 设置
    settings = "settings",
    // 每日签到
    daily = "daily",
    // 转盘数据
    wheelData = "wheelData",
    // 商店数据
    storeData = "storeData",
    // ad数据
    adData = "adData",
    passData = "passData",

    first_fill_ad_info_i = 'first_fill_ad_info_i',
    first_fill_ad_info_v = 'first_fill_ad_info_v',
    // 支付数据
    payItem = 'payItem',
    
    // abtest
    abTestData = 'abTestData',

    //review 评价 1好评 2差评/未评价
    review = 'review',
    //review_time 弹评价时间戳
    review_time = 'review_time',

    // 放置礼物
    placeGiftsReceiveTime = 'placeGiftsReceiveTime',
}

export default class LocalValues {
    // static {
    //     if (!this.getUserData()) {
    //         this.setMusic(true);
    //         this.setSound(true);
    //         this.setShake(true);
    //     }
    // }
    //============基础功能部分============
    public static setObject(key: string, value: any) {
        sys.localStorage.setItem(key, JSON.stringify(value));
    }
    public static getObject<T>(key: string) {
        const json = sys.localStorage.getItem(key);
        if (!json) return null;
        return <T>JSON.parse(sys.localStorage.getItem(key));
    }
    public static setNumber(key: string, value: number) {
        sys.localStorage.setItem(key, value.toString());
    }
    public static getNumber(key: string) {
        return parseInt(sys.localStorage.getItem(key) || '0');
    }
    public static setString(key: string, value: string) {
        sys.localStorage.setItem(key, value);
    }
    public static getString(key: string) {
        return sys.localStorage.getItem(key);
    }
    public static setBoolean(key: string, value: boolean) {
        sys.localStorage.setItem(key, value.toString());
    }
    public static getBoolean(key: string) {
        return sys.localStorage.getItem(key) === 'true';
    }
    public static setJson(key: string, value: any) {
        sys.localStorage.setItem(key, JSON.stringify(value));
    }
    public static getJson(key: string) {
        const json = sys.localStorage.getItem(key);
        if (!json) return null;
        console.log('### LocalValues getJson', json);
        return JSON.parse(json);
    }
    public static removeItem(key: string) {
        sys.localStorage.removeItem(key);
    }
    public static clear() {
        sys.localStorage.clear();
    }
    //============游戏业务部分============
    public static setUserData(data: any) {
        this.setObject(LocalKey.userData, data)
    }
    public static getUserData():IUserData {
        return this.getObject(LocalKey.userData)
    }
    public static setLanguage(lang: string) {
        this.setString(LocalKey.language, lang)
    }
    public static getLanguage() {
        return this.getString(LocalKey.language)
    }
    public static getCount(key: string) {
        return this.getNumber(key) || 0;
    }
    public static addCount(key: string, add: number = 1) {
        let count = this.getCount(key) + add;
        this.setNumber(key, count);
        return count;
    }
}